将ICU Unicode字符串转换为std::wstring (或wchar

您所在的位置:网站首页 uchar char 将ICU Unicode字符串转换为std::wstring (或wchar

将ICU Unicode字符串转换为std::wstring (或wchar

2024-05-24 07:19| 来源: 网络整理| 查看: 265

对于std::wstring,C++标准没有规定任何特定的编码。在Windows系统上,wchar_t是16位的,而在Linux、macOS和其他几个平台上,wchar_t是32位的。就C++的std::wstring而言,它只是一个任意的wchar_t序列,就像std::string是一个任意的char序列一样。

icu::UnicodeString似乎没有内置的创建std::wstring的方法,但是如果你真的想创建std::wstring,你可以像这样使用基于C的接口u_strToWCS():

代码语言:javascript复制icu::UnicodeString ustr = /* get from somewhere */; std::wstring wstr; int32_t requiredSize; UErrorCode error = U_ZERO_ERROR; // obtain the size of string we need u_strToWCS(nullptr, 0, &requiredSize, ustr.getBuffer(), ustr.length(), &error); // resize accordingly (this will not include any terminating null character, but it also doesn't need to either) wstr.resize(requiredSize); // copy the UnicodeString buffer to the std::wstring. u_strToWCS(wstr.data(), wstr.size(), nullptr, ustr.getBuffer(), ustr.length(), &error);

据推测,u_strToWCS()将使用最有效的方法将UChar转换为wchar_t (如果它们的大小相同,那么我认为它只是一个简单的副本)。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3